package cn.iocoder.yudao.module.digital.service.thesis;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.digital.controller.admin.thesis.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.thesis.ThesisDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.digital.dal.mysql.thesis.ThesisMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.digital.enums.ErrorCodeConstants.*;

/**
 * 论文 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class ThesisServiceImpl implements ThesisService {

    @Resource
    private ThesisMapper thesisMapper;

    @Override
    public Integer createThesis(ThesisSaveReqVO createReqVO) {
        // 插入
        ThesisDO thesis = BeanUtils.toBean(createReqVO, ThesisDO.class);
        thesisMapper.insert(thesis);
        // 返回
        return thesis.getId();
    }

    @Override
    public void updateThesis(ThesisSaveReqVO updateReqVO) {
        // 校验存在
        validateThesisExists(updateReqVO.getId());
        // 更新
        ThesisDO updateObj = BeanUtils.toBean(updateReqVO, ThesisDO.class);
        thesisMapper.updateById(updateObj);
    }

    @Override
    public void deleteThesis(Integer id) {
        // 校验存在
        validateThesisExists(id);
        // 删除
        thesisMapper.deleteById(id);
    }

    private void validateThesisExists(Integer id) {
        if (thesisMapper.selectById(id) == null) {
            throw exception(THESIS_NOT_EXISTS);
        }
    }

    @Override
    public ThesisDO getThesis(Integer id) {
        return thesisMapper.selectById(id);
    }

    @Override
    public PageResult<ThesisDO> getThesisPage(ThesisPageReqVO pageReqVO) {
        return thesisMapper.selectPage(pageReqVO);
    }

}